var expense = {
    showConfirmMsg: function()
    {
        if (confirm('Are you sure you want to delete this expense?') == false) {
            return false;
        }
    },
    copyExpense: function(url_copy) 
    {
        $('#copyPlannedExpense').click(function() {
            if (confirm('Are you sure you copy planned expense of the previous month?')) {
                var date = $('#select-month').val();
                if(!date) {
                    date = moment().format('MM.YYYY');
                } 

                $.post(url_copy,
                    { date: date},
                    function(data) {
                        if (data.status) {
                            $('#flash-alert').append("<div class='alert alert-success' role='alert'>Expenses planned has copied!</div>");
                            $('#flash-alert div').fadeOut(4000, function() {
                            });
                            $('#list_planned_expenses').jqGrid('setGridParam',{ postData:{ 'date': date }, search:true });
                            $('#list_planned_expenses').trigger("reloadGrid");
                        } else {
                            $('#flash-alert').append("<div class='alert alert-danger' role='alert'>Not copied!</div>");
                            $('#flash-alert div').fadeOut(4000, function() {
                            });
                        }
                    }
                );
            }
        });    
    },
    selectMonth: function(url_data, chart) 
    {
        var today = moment().format('MM.YYYY');
        $('#select-month').val(today);  
        $('#select-month-to').val(today); 
        $('#select-month-from').val(today); 
        
        $('.select-month-left').click(function() {
            $('.small-ajax-loader').show();
            
            var id = $(this).parent('.col-md-4').find('input').attr('id');
            changeMonth('left', id);
            
           $('.small-ajax-loader').hide(); 
        });
        
        $('.select-month-right').click(function() {
            $('.small-ajax-loader').show();
            
            var id = $(this).parent('.col-md-4').find('input').attr('id');
            changeMonth('right', id);
            
            $('.small-ajax-loader').hide(); 
        });
        
        function changeMonth(where, id) {
            $('.small-ajax-loader').show();
            var data  = $('#' + id).val();
            var myarr = data.split(".");
            var month = parseInt(myarr[0], 10);
            var year  = parseInt(myarr[1], 10);
            if(where === 'left') {
                if (month == 1) {
                   month = 12;
                   year -= 1;
                } else {
                    month -= 1;
                }
            } else if(where === 'right') {
                 if (month == 12) {
                    month = 1;
                    year += 1;
                 } else {
                     month += 1;
                 }   
            }
            if (month < 10) {
                month = '0' + month;
            }

            $('#' + id).val(month + "." + year); 
           
            if(id === 'select-month') {
                //$('#list').jqGrid('setGridParam',{ postData:{ 'month': year + '-' + month }, search:true });
                $('#list').jqGrid('setGridParam',{ postData:{ 'date': month + '.' + year }, search:true });
                $('#list').trigger("reloadGrid");
                $('#list_planned_expenses').jqGrid('setGridParam',{ postData:{ 'date': month + '.' + year }, search:true });
                $('#list_planned_expenses').trigger("reloadGrid");
            } else if (id == 'select-month-to' || id == 'select-month-from') {
                chart.refresh('chart1', null, url_data);
            }
            $('.small-ajax-loader').hide(); 
        }
        
        $('body').on('focus', '#select-month', function() {
            datePickerFnk(this, null);
        });
        
        $('body').on('focus', '#select-month-to', function() {
            datePickerFnk(this, 'chart');
        }); 
        
        $('body').on('focus', '#select-month-from', function() {
            datePickerFnk(this, 'chart');
        }); 
        
        function datePickerFnk(d, type) {
            $(d).datetimepicker({
                "format": "mm.yyyy", 
                "pickerPosition": "bottom-left", 
                "minView": "year", 
                "startView": "year",
                "autoclose": true
            }).on('changeMonth', function(ev){
               var month = ev.date.getMonth() + 1; 
               var year  = ev.date.getFullYear(); 
               if (month < 10) {
                   month = '0' + month;
               }
               
               if (type === 'chart') {
                   chart.refresh('chart1', null, url_data);
               } else {
                    //$('#list')[0].clearToolbar();
                    $('#list').jqGrid('setGridParam',{ postData:{ 'date': month + '.' + year }, search:true });
                    $('#list').trigger("reloadGrid");
                    $('#list_planned_expenses').jqGrid('setGridParam',{ postData:{ 'date': month + '.' + year }, search:true });
                    $('#list_planned_expenses').trigger("reloadGrid");
               }
           });
        }
    }
}
